package com.ntt.benew.dictionary.view

import android.content.Context
import android.graphics.Typeface
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.ntt.benew.dictionary.R
import com.ntt.benew.dictionary.entity.gpt.ChunkingEntity
import com.ntt.benew.dictionary.entity.gpt.WordCoreEntity
import com.ntt.benew.dictionary.listener.TxtOnClickListener
import com.ntt.benew.dictionary.utils.setGradientDrawableOnlyStroke

/**
 * Created by Android Studio.
 * User: tw
 * Date: 2023/11/2
 * Time: 12:14
 * 单词基础组件
 */
class WordCallView(context: Context) : LinearLayout(context) {

    private val titleColor: Array<Int> = arrayOf(
        R.color.ntt_FD8D8D,
        R.color.ntt_FFA34C,
        R.color.ntt_3FD4BA,
        R.color.ntt_87B5FF,
    )

    /**
     * 每一个单词文本的点击事件
     */
    var mTextClickListener: TxtOnClickListener? = null

    init {
        orientation = VERTICAL

    }


    fun setData(chunkingEntity: ChunkingEntity, datas: MutableList<WordCoreEntity>) {
        createView(chunkingEntity, datas)

    }


    private fun createView(chunkingEntity: ChunkingEntity, datas: MutableList<WordCoreEntity>) {
        val t = TextView(context).apply {
            text = chunkingEntity.translation
            setTextColor(ContextCompat.getColor(context, getSvoColor(chunkingEntity.translation ?: "")))
            textSize = 18f
        }
        addView(t)
        val contentLayout = LinearLayout(context)
        datas.forEach {
            val content = TextView(context).apply {
                text = "${it.word} "
                textSize = 28f
                setTextColor(
                    ContextCompat.getColor(
                        context,
                        if (it.isCore) R.color.ntt_FF0000 else R.color.ntt_000000
                    )
                )
                typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                setTag(R.id.tag_word, it.word)
                setTag(R.id.tag_breakdown, chunkingEntity)

            }
            content.setOnClickListener {
                mTextClickListener?.onTxtClick(
                    it.getTag(R.id.tag_word) as String,
                    it.getTag(R.id.tag_breakdown) as ChunkingEntity
                )
            }
            contentLayout.addView(content)
        }


        contentLayout.setGradientDrawableOnlyStroke(
            0,
            ContextCompat.getColor(context, getSvoColor(chunkingEntity.translation ?: "")),
            1
        )
        addView(contentLayout)

    }

    private fun getSvoColor(type: String): Int {
        return when {
            type.contains("主语") -> titleColor[0]
            type.contains("谓语") -> titleColor[1]
            type.contains("宾语") -> titleColor[2]
            else -> titleColor[3]
        }
    }
}